/****************************************************************************
**
** Copyright (C) 2009 by user *
** user@greenphonesdk *
**
** Contact: Qt Extended Information (info@qtextended.org)
**
** This file may be used under the terms of the GNU General Public License
** version 2.0 as published by the Free Software Foundation and appearing
** in the file LICENSE.GPL included in the packaging of this file.
**
** Please review the following information to ensure GNU General Public
** Licensing requirements will be met:
**     http://www.fsf.org/licensing/licenses/info/GPLv2.html.
**
****************************************************************************/

#include "addcontactdialog.h"


AddContactDialog::AddContactDialog(QWidget *parent, Qt::WFlags f)
    : QWidget(parent, f)
{
    // initialize the dialog
	setupUi(this);	
	entry = NULL;
	currentItem=LED;
	pictureFilePath = "";
}

AddContactDialog::~AddContactDialog()
{
    // Do nothing
}

void AddContactDialog::setupEdit(NeoPhoneBookEntry *entryToEdit)
{
    entry = entryToEdit;
	nameLineEdit->setText(entryToEdit->getContactName());
	numberLineEdit->setText(entryToEdit->getPhoneNumber());
	emailLineEdit->setText(entryToEdit->getContactEmail());
	pictureFilePath = entryToEdit->getPictureFilePath();
	if (!pictureFilePath.isEmpty()) {
		QDir dir = QDir::home();
		dir.cd("Documents_PC");
		QPixmap pixmap(dir.filePath(pictureFilePath));
		pixmap = pixmap.scaled(QSize(pictureLabel->width(), 
				pictureLabel->height()), Qt::KeepAspectRatio,
				Qt::SmoothTransformation);
		pictureLabel->setPixmap(pixmap);
		removePushButton->setEnabled(true);
	}
	
	ringToneFilePathLabel->setText(entryToEdit->getRingToneFilePath());
	vibrationPatternFilePathLabel->setText(entryToEdit->getVibrationPatternFilePath());
	ledPatternFilePathLabel->setText(entryToEdit->getLedPatternFilePath());
	ringOptionComboBox->setCurrentIndex(entryToEdit->getRingOption());
	ringOffsetLabel->setText(QString::number(entryToEdit->getRingOffset()));
}


void AddContactDialog::on_savePushButton_clicked() {
	QString name = nameLineEdit->text();
	QString number = numberLineEdit->text();
	QString email = emailLineEdit->text();
	QString ringTone = ringToneFilePathLabel->text();
	QString vibration = vibrationPatternFilePathLabel->text();
	QString led = ledPatternFilePathLabel->text();
	int option = ringOptionComboBox->currentIndex();
	int offset = ringOffsetLabel->text().toInt();
	
	if (name.isEmpty() || number.isEmpty()) {
		QMessageBox::warning(this, "Missing Info.",
				"Name and phone number are required.", QMessageBox::Ok);
	} else {
		if (entry == NULL) { // new contact
			entry = new NeoPhoneBookEntry();			
		}
		entry->setContactName(name);
		entry->setPhoneNumber(number);
		entry->setContactEmail(email);
		entry->setPictureFilePath(pictureFilePath);
		entry->setRingToneFilePath(ringTone);
		entry->setVibrationPatternFilePath(vibration);
		entry->setLedPatternFilePath(led);
		entry->setRingOption(option);
		entry->setRingOffset(offset);
		emit updateContactSignal(entry);
		close();
	}
}
void AddContactDialog::on_cancelPushButton_clicked() 
{
	close();
}

void AddContactDialog::on_removePushButton_clicked() 
{
	pictureFilePath = "";
	pictureLabel->setText("No Picture");
	removePushButton->setEnabled(false);
}
void AddContactDialog::on_browsePushButton_clicked() 
{
	QString dir = "~/Documents_PC/" + pictureFilePath;
	QString fileName = QFileDialog::getOpenFileName(this,
     tr("Open Image"), dir, tr("Image Files (*.png *.jpg *.bmp)"));

	if (fileName != NULL) {
		QPixmap pixmap = QPixmap(fileName);
		pictureFilePath = fileName.right(fileName.length()-fileName.lastIndexOf("/")-1);
		if (!pixmap.isNull()) {
			pixmap = pixmap.scaled(QSize(pictureLabel->width(), 
					 pictureLabel->height()), Qt::KeepAspectRatio,
					 Qt::SmoothTransformation);
			pictureLabel->setPixmap(pixmap);
			removePushButton->setEnabled(true);
		}
	}
}

void AddContactDialog::on_ringToneBrowsePushButton_clicked()
{	
	currentItem = RINGTONE;	
	//qDebug() << "Ring Tone Browsing ..." << currentItem;
	openSelectCallDataDialog();	
}

void AddContactDialog::on_vibrationBrowsePushButton_clicked()
{
	currentItem = VIBRATION;
	//qDebug() << "Vibration Pattern Browsing ..." << currentItem;
	openSelectCallDataDialog();
}

void AddContactDialog::on_ledBrowsePushButton_clicked()
{
	currentItem = LED;
	//qDebug() << "LED Pattern Browsing ..." << currentItem;
	openSelectCallDataDialog();	
}

void AddContactDialog::openSelectCallDataDialog()
{
	qDebug() << "AddContactDialog::openSelectCallDataDialog() |";
	SelectCallDataDialog *dialog = new SelectCallDataDialog(this);
	dialog->setAttribute(Qt::WA_DeleteOnClose);		
	QObject::connect(dialog, 
				SIGNAL(fileSelectedSignal(QString)), 
				this, SLOT(updateFilePath(QString)));	
	QObject::connect(this, 
				SIGNAL(closeSignal()), 
				dialog, SLOT(closeListener()));	

	switch (currentItem) {
	case RINGTONE:
		dialog->setWindowTitle("Rint Tone (*.aud)");
		dialog->showList("Default", ".aud", ringToneFilePathLabel->text(), "");
		dialog->showMaximized();
		break;
	case VIBRATION:
		dialog->setWindowTitle("Vibration Pattern (*.vib)");
		dialog->showList("Pulse", ".vib", vibrationPatternFilePathLabel->text(), 
				"Random");
		dialog->showMaximized();
		break;
	case LED:
		dialog->setWindowTitle("LED Pattern (*.led)");
		dialog->showList("Pulse", ".led", ledPatternFilePathLabel->text(), "Random");
		dialog->showMaximized();
		break;
	default:
		qDebug() << "Not support Call_DataTypes [unknown]";
		dialog->close();
		break;
	}
}

void AddContactDialog::updateFilePath(QString path) 
{	
	//qDebug() << "Update path for" << path;
	QString prevRingTone;
	QMessageBox msgBox;
	switch(currentItem) {
	case RINGTONE:
		//qDebug() << "update ring tone";		
		prevRingTone = ringToneFilePathLabel->text();
		if (prevRingTone.compare(path) != 0) {
			ringOffsetLabel->setText("0");
		}
		ringToneFilePathLabel->setText(path);		
        msgBox.setWindowTitle("Ring Tone Offset");
        msgBox.setIcon(QMessageBox::Information);
        msgBox.setText("Do you want to set ring tone offset?");
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        switch (msgBox.exec()) {	
        case QMessageBox::Yes:
            openRingOffsetDialog(true);	
            break;
        case QMessageBox::No:
        	break;
    	default:
        	break;
    	}
		break;
	case VIBRATION:
		//qDebug() << "update vibration";
		vibrationPatternFilePathLabel->setText(path);
		break;
	case LED:
		//qDebug() << "update LED";
		ledPatternFilePathLabel->setText(path);
		break;
	default:
		qDebug() << "No current item selected";
		break;
	}	
}

void AddContactDialog::on_ringOptionComboBox_currentIndexChanged(int index)
{
	if (index == 0) {
		ledBrowsePushButton->setEnabled(true);
	} else {
		ledBrowsePushButton->setEnabled(false);
	}
}

void AddContactDialog::on_ringOffsetPushButton_clicked()
{	
	qDebug() << "ring offset setting ...";
	openRingOffsetDialog(false);	
}

void AddContactDialog::updateOffset(int offset)
{
	ringOffsetLabel->setText(QString::number(offset));
}

void AddContactDialog::openRingOffsetDialog(bool fromSelectDialog)
{
	AudioDialog *dialog = new AudioDialog(this);
	dialog->setAttribute(Qt::WA_DeleteOnClose);		
	QObject::connect(dialog, 
				SIGNAL(setOffsetSignal(int)), 
				this, SLOT(updateOffset(int)));	
	QObject::connect(this, 
				SIGNAL(closeSignal()), 
				dialog, SLOT(closeListener()));	
	if (fromSelectDialog) {
		QObject::connect(dialog, 
				SIGNAL(cancelSignal()), 
				this, SLOT(openSelectCallDataDialog()));	
	}	
	dialog->setWindowTitle("Audio Player");
	QString audioname = ringToneFilePathLabel->text();
	if (!audioname.endsWith(".aud")) {
		audioname = audioname + ".aud";
	}
	dialog->setAudio(audioname, ringOffsetLabel->text().toInt());
	dialog->showMaximized();
}

void AddContactDialog::closeListener(int condition)
{
	emit closeSignal();
	close();
}




